package livraria.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import livraria.dao.LivrariaDaoException;

public class ConnectionLivrariaFactory {

	public static Connection getConnection() throws LivrariaDaoException
	{
		
		try{
			Class.forName("com.mysql.jdbc.Driver");
			return DriverManager.getConnection("jdbc:mysql://localhost/livraria","root","");
		}
		catch(Exception e)
		{
			throw new LivrariaDaoException(e.getMessage());
		}
		
	}
	
	
	public static void closeConnection(Connection con,Statement stm, ResultSet rs) throws LivrariaDaoException
	{
		close(con, stm, rs);
	}
	
	public static void closeConnection(Connection con,Statement stm) throws LivrariaDaoException
	{
		close(con, stm, null);
	}
	
	public static void closeConnection(Connection con) throws LivrariaDaoException
	{
		close(con, null, null);
	}
	
	private static void close(Connection con,Statement stm, ResultSet rs) throws LivrariaDaoException
	{
		try
		{
			if(rs != null) rs.close();
			if(stm != null) stm.close();
			if(con != null) con.close();
		}
		catch(Exception e)
		{
			throw new LivrariaDaoException(e.getMessage());
		}
	}
	
}
